/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package systemsim;

import java.util.Random;

/**
 *
 * @author Carolina
 */
public class Group {

    private int ID;
    private int numPeople;
    private int averageEatingTime; //From user
    private int eatingTime; //Random generation based on averageEatingTime
    private int numTables;
    private int patience;
    private double timeInLine;
    long timeWhenFoodReceived = System.currentTimeMillis()+1000L*3600L*1000L; //create a large value, the system is dependent 
                                                                                   //on this in the cases when the food has not yet 
                                                                                   //been received and this value is used to calculate 
                                                                                   //whether or not the group is ready to leave
    
    public enum Status {INLINE, SEATED, WAITINGONFOOD, EATING}
    
    public Status state;
    
        
    private Random rand = new Random();

    public Group(int ID, int numPeople, int averageEatingTime)
    {
        this.ID = ID;
        this.numPeople = numPeople;
        this.eatingTime = generateEatingTime(averageEatingTime);
        patience = rand.nextInt(5); //Maybe more values
        numTables = totalTableNumber(numPeople);
        state = Status.INLINE;
    }
    
    public int getID() {
        return ID;
    }

    public void setID(int ID) {
        this.ID = ID;
    }

    public double getAverageEatingTime() {
        return averageEatingTime;
    }

    public void setAverageEatingTime(int averageEatingTime) {
        this.averageEatingTime = averageEatingTime;
    }

    public int getEatingTime() {
        return eatingTime;
    }
    
    public long getTimeOfDepartureMilliseconds() {
        
        long eatTime = (long)this.eatingTime*60L*1000L; //in milliseconds
        //System.out.println("EatTime="+eatTime);
        //System.out.println("EatTimeScaled="+(eatTime/Main.TIMESCALE));
        
        return eatTime/Main.TIMESCALE + this.timeWhenFoodReceived;
    }

    public void setTimeWhenFoodReceived(long timeWhenFoodReceived) {
        this.timeWhenFoodReceived = timeWhenFoodReceived;
    }
    

    public void setEatingTime(int eatingTime) {
        this.eatingTime = generateEatingTime(eatingTime);
    }

    public int getNumPeople() {
        return numPeople;
    }

    public void setNumPeople(int numPeople) {
        this.numPeople = numPeople;
    }

    public double getPatience() {
        return patience;
    }

    public void setPatience(int patience) {
        this.patience = patience;
    }

    public double getTimeInLine() {
        return timeInLine;
    }

    public void setTimeInLine(double timeInLine) {
        this.timeInLine = timeInLine;
    }

    public int getNumTables() {
        return numTables;
    }

    public void setNumTables(int numTables) {
        this.numTables = numTables;
    }

    @Override
    public String toString() {
        return "Group{" + "ID=" + ID + "  numOfppl="+this.numPeople+'}';
    }
    
    

    //Method to get +- eating time
    private int generateEatingTime(int averageEatingTime)
    {
        int i = rand.nextInt(2);
        int k = rand.nextInt(5);
        if(i < 1)
        {
           return averageEatingTime + k;
        }
        else
        {
            return averageEatingTime - k;
        }
    }

    //Method to get number of tables
    private int totalTableNumber(int numPeople)
    {
        if(numPeople%4 > 0)
        {
            return numPeople/4 + 1;
        }
        else if(numPeople/4 == 0)
        {
            return 1;
        }
        else
        {
            return numPeople/4;
        }
    }    
}
